<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('IN_COMPONENT_CONTROLLER') or die();

require_once('includes/functions/functions.php');

class TellAFriendEntryComponent
{
	function TellAFriendEntryComponent()
	{
	}


	function action(&$params, $action='query')
	{
		if ($action == 'TellAFriend') return $this->send($params);
		else return $this->query($params);
	}

	function query(&$params) 
	{
		$errorCode = (int)$params['taf_error_code'];

		$guestAllowed = returnBoolean(ALLOW_GUEST_TO_TELL_A_FRIEND);
		$value{'TellAFriendEntryComponentMetaData'}{'allow_guest_to_tell_a_friend'} = $guestAllowed;
		$value{'TellAFriendEntryComponent'}{0}{'error_code'} = $errorCode;

		return $value;
	}

	function send($params) 
	{
		$account_id = (int)$params['accountId'];
		$product_id = (int)$params['productId'];
		$language_id = (int)$params['languageId'];
		$from_email_address = prepare_input($params['your_email_address']);
		$from_name = prepare_input($params['your_name']);
		$to_email_address = prepare_input($params['friends_email_address']);
		$to_name = prepare_input($params['friends_name']);
		$message = prepare_input($params['your_message']);

		$guestAllowed = returnBoolean(ALLOW_GUEST_TO_TELL_A_FRIEND);
		$errorCode = 0;

  		$valid_product = false;

		$sqlQuery = 'select pd.products_name, ptc.categories_id from ' . TABLE_PRODUCTS . ' p, ' . TABLE_PRODUCTS_DESCRIPTION . ' pd, ' . TABLE_PRODUCTS_TO_CATEGORIES . ' ptc ' .
		'where p.products_status = \'1\' and p.products_id = \'' . $product_id . '\' and p.products_id = pd.products_id ' .
		'and pd.language_id = \'' . $language_id . '\' ' .
		'and p.products_id = ptc.products_id';

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		while($row = mysql_fetch_array($dataReturned))
		{
			$product_name = $row['products_name'];
			$category_id = $row['categories_id'];
      			$valid_product = true;
		}

		if (!$valid_product)
		{
			$errorCode = -10;
		}

		if ($errorCode == 0 && !$guestAllowed && $account_id < 1)
		{
			$errorCode = -20;
		}

		if ($errorCode == 0 && !validate_email_format($from_email_address, true))
		{
			$errorCode = -30;
		}
			
		if ($errorCode == 0 && !validate_email_format($to_email_address, true))
		{
			$errorCode = -40;
		}
			
		if ($errorCode == 0 && strlen($from_name) < 1)
		{
			$errorCode = -50;
		}
			
		if ($errorCode == 0 && strlen($to_name) < 1)
		{
			$errorCode = -60;
		}	
			
		if ($errorCode == 0)
		{

			if ($account_id > 0)
			{
				$from_name = get_account_name($account_id);
				$accountEmailAddress = get_account_email($account_id);
				if (validate_email_format($accountEmailAddress, true))
				{
					$from_email_address = $accountEmailAddress;
				}
			}
			//Send
      			$email_subject = sprintf(TAF_TEXT_EMAIL_SUBJECT, $from_name, STORE_NAME);
      			$email_body = sprintf(TAF_TEXT_EMAIL_INTRO, $to_name, $from_name, $product_name, STORE_NAME) . "\n\n";

      			if ($message != null && strlen($message) > 0) {
        			$email_body .= $message . "\n\n";
     			}

      			$email_body .= sprintf(TAF_TEXT_EMAIL_LINK, HTTP_SERVER . '#ProductComponent--' . $product_id . '--' . $category_id . '--0' . "\n\n") .
                     			sprintf(TAF_TEXT_EMAIL_SIGNATURE, STORE_NAME . "\n" . HTTP_SERVER . "\n");

			send_email($to_name, $to_email_address, $email_subject, $email_body, $from_name, $from_email_address);
			$errorCode = 10;
		}
		$params['taf_error_code'] = $errorCode;
		return $this->query($params);
	}
}
?>
